﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace TSFileOutput
{
    public class Program
    {
        static void Main(string[] args)
        {
            StringBuilder LogBuilder = new StringBuilder();
            int StepsCounter = 0;

            if (TSConfigurator.ReadConfiguration())
            {

                LogBuilder.AppendLine((StepsCounter++) + ". FileOutput start watching.");
                // Generate normal file                
                FileGenerator CurGenerator = new FileGenerator(TSConfigurator.PathForOutputFiles, TSConfigurator.PathForArchievedFiles, TSConfigurator.DatabaseConnection, TSConfigurator.DatabaseType, TSConfigurator.Email_Active, TSConfigurator.SMTP);
                CurGenerator.GenerateMyPending4Approval(TypeOfFile.ColumnLengthOriented);

                for (int x = 0; x < CurGenerator.LoggedMessages.Count; x++)
                {
                    LogBuilder.AppendLine((StepsCounter++) + ". " + CurGenerator.LoggedMessages[x].ToString());
                }

                LogBuilder.AppendLine((StepsCounter++) + ". Time Entry Pending for Approval File End");
                CurGenerator = null;
            }
            else
            {
                LogBuilder.AppendLine((StepsCounter++) + ". Error reading configuration : " + TSConfigurator.ErrMsg);
            }

            LogBuilder.AppendLine((StepsCounter++) + ". Exit. Good Bye.");

            /* The End */
            string NewFolderName = DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year;
            string NewFileName = "log_" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + ".txt";

            if (!Directory.Exists(TSConfigurator.PathForLogFiles + "\\" + NewFolderName))
                Directory.CreateDirectory(TSConfigurator.PathForLogFiles + "\\" + NewFolderName);

            TSConfigurator.FileCreate(TSConfigurator.PathForLogFiles + "\\" + NewFolderName + "\\" + NewFileName, Encoding.ASCII.GetBytes(LogBuilder.ToString()));

            Console.WriteLine(LogBuilder.ToString());

            // clean up
            LogBuilder = null;
        }
    }
}
